/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function() {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('name')] = param;
    });
    return searchParams;
}

function getSearchParams2(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    return searchParams;
}
//清空搜索条件
$("#Reset").click(function() {
    $('.SearchForm').val('');
    $("input:radio[name='sex']").removeAttr('checked');
});

/**
 * 页面自加载
 */
$(function() {

    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_groupsinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_groupsinfo').datagrid({
        singleSelect: true, //允许选择多行
        striped: true,
        idField: 'g_id',
        remoteSort: false,
        collapsible: true,
        fit: false,
        url: 'admin/Groups/getData', //数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[{
            field: 'g_name',
            title: '群名称',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_username',
            title: '群主',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'user_phone',
            title: '群主电话',
            align: 'center',
            width: cellwidth,
            sortable: true
        }, {
            field: 'g_thumb',
            title: '群头像',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value){
                return "<img src='"+value+"' height='50' />";
            }
        }, {
            field: 'g_status',
            title: '群状态',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value){
                if(value == 1){
                    return "<span style='color:green;'>正常</span>";
                }else{
                    return "<span style='color:red;'>已解散</span>";
                }
            }
        }, {
            field: 'g_uids',
            title: '群员数量',
            align: 'center',
            width: cellwidth,
            sortable: true,
            formatter:function(value){
                if(value != ''){
                    var val = value.slice(1,value.length-1);
                    var arr = val.split(',');
                    return arr.length;
                }else{
                    return "0";
                }
            }
        }, {
            field: 'g_time',
            title: '群创建时间',
            align: 'center',
            width: cellwidth,
            sortable: true
        }]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });

    $('#Search').click(function() {
        $('#datatable_groupsinfo').datagrid('reload');
    });

    /**
     * 这是解散不是删除
     */
    $('#del').click(function() {
        var row = $('#datatable_groupsinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        if (row[0].g_status == 2) {
            $.TeachDialog({
                content: '此群已经解散了！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认解散此群？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Groups/delete', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "uid=" + row[0].user_id+"&gid=" + row[0].g_id,
                    success: function(data) {
                        if (data.status == 200) {
                            layer.msg(data.msg,{icon:6,time:2000});
                            $('#datatable_groupsinfo').datagrid('reload');
                            $('#datatable_groupsinfo').datagrid('uncheckAll');
                        } else {
                            layer.msg(data.msg,{icon:5,time:2000});
                        }
                    },
                    error: function() {
                        layer.msg("解散失败!",{icon:5,time:2000});
                    }
                });
            }
        })
    });

    /**
     * 查看群组详情
     */
    $("#look").click(function() {
        var row = $('#datatable_groupsinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个群组进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一个群组进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/Groups/seeInfo",
            data:"gid=" + row[0].g_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
//                        largeSize:true,
                    title: "<h2>群组详情</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });
    /**
     * 这是删除
     */
    $('#cirmes').click(function() {
        var row = $('#datatable_groupsinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        $.TeachDialog({
            content: "确认删除此群？",
            showCloseButton: true,
            showCloseButtonName: '取消',
            otherButtons: ['确认'],
            otherButtonStyles: ['btn-danger'],
            clickButton: function(sender, modal, index) {
                modal.modal('hide');
                $.ajax({
                    url: 'admin/Groups/deletegroup', //form action
                    dataType: 'JSON', //返回体类型
                    type: 'POST', // form type
                    data: "uid=" + row[0].user_id+"&gid=" + row[0].g_id + "&status=" + row[0].g_status,
                    success: function(data) {
                        if (data.status == 200) {
                            layer.msg(data.msg,{icon:6,time:2000});
                            $('#datatable_groupsinfo').datagrid('reload');
                            $('#datatable_groupsinfo').datagrid('uncheckAll');
                        } else {
                            layer.msg(data.msg,{icon:5,time:2000});
                        }
                    },
                    error: function() {
                        layer.msg("解散失败!",{icon:5,time:2000});
                    }
                });
            }
        })
    });












    /**
     * 查看动态的回复
     */
    $("#cirmes2222").click(function() {
        var row = $('#datatable_groupsinfo').datagrid('getSelections');
        if (row.length < 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！'
            });
            return;
        }
        if (row.length > 1) {
            $.TeachDialog({
                content: '请选择一个用户进行查看！',
            });
            return;
        }
        //获得修改框内容
        var content = "";
        $.ajax({
            url:"admin/Circle/myCirs",
            data:"cid=" + row[0].circle_id,
            type:'POST',
            dataType:'HTML',
            success:function(returndata){
                content = returndata;
                $.TeachDialog({
                    modalId: null,
                    animation: null,
                    largeSize:true,
                    title: "<h2>评论回复</h2>",
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    CloseButtonAddFunc: function () {
                    }
                });
                //显示评论列表
                var psval = $('#datatable_groupsinfo2').attr('data-size');
                if (psval == undefined || psval == "") {
                    psval = 10;
                }
                var cid = $('#cid').val();
                $('#datatable_groupsinfo2').datagrid({
                    singleSelect: true, //允许选择多行
                    striped: true,
                    idField: 'comment_id',
                    remoteSort: false,
                    collapsible: true,
                    fit: false,
                    url: 'admin/Circle/myCirsTable', //数据源路径
                    queryParams:{
                      cid:cid
                    },
                    loadMsg: '请等待数据载入....',
                    pagination: true,
                    rownumbers: true,
                    fitColumns: true,
                    pageSize: psval,
                    pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
                    columns: [[{
                        field: 'one_name',
                        title: '评论者名字',
                        align: 'center',
                        width: cellwidth,
                        sortable: true,
                        formatter: function (value,row,index) {
                            if(row.reply_name){
                                return "<span style='color: gray;'>"+row.reply_name+"</span>&nbsp;&nbsp;回复&nbsp;&nbsp;<span style='color: gray;'>"+value+"</span>";
                            }else{
                                return value;
                            }
                        }
                    }, {
                        field: 'cm_content',
                        title: '评论内容',
                        align: 'center',
                        width: cellwidth,
                        sortable: true,
                    }, {
                        field: 'cm_time',
                        title: '评论时间',
                        align: 'center',
                        width: cellwidth*1.3,
                        sortable: true,
                    }, {
                        field: 'cm_id',
                        title: '操作',
                        align: 'center',
                        width: cellwidth*1.5,
                        sortable: true,
                        formatter:function(value,row,index){
                            var c = row.cm_content;
                            return "<a onclick='kancir(this);' style='margin-left:5px;font-weight: bold;' name='"+c+"' href='JavaScript:;'>回复详情</a>"+
                             "<a onclick='deleteCirmes(this);' style='margin-left:5px;font-weight: bold;color: red' name='"+value+"' href='JavaScript:;'>删除</a>";
                        }
                    }]],
                    onBeforeLoad: function (param) {
                        param = getSearchParams2(param);
                    },
                });
                //=============================================
            },
            error:function(){
                alert('数据返回出错，无伤大雅，请继续');
            }
        });

    });



});